package org.ymf.elasticsearch;

import javax.annotation.Resource;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.layuiframework.stereotype.elasticsearch.ElasticsearchHandler;
import org.layuiframework.stereotype.elasticsearch.ElasticsearchRecord;
import org.layuiframework.stereotype.elasticsearch.ElasticsearchVo;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.ymf.service.ChartService;
import org.ymf.vo.ChartVo;
import java.util.List;

/**
 * <p>
 * 图表信息表 => Elasticsearch聚合查询(key: Chart)
 * </p>
 *
 * @author Malphite
 * @since 2023-08-01
 */
@Slf4j
@Component
public class ChartElasticsearchHandler implements ElasticsearchHandler {

    @Resource
    private ChartService chartService;

    private String type = "Chart";

    @Override
    public String getType() {
        return this.type;
    }

    @Override
    public boolean canDeal(ElasticsearchVo vo) {
        return !StringUtils.hasLength(vo.getType()) || this.type.equals(vo.getType());
    }

    @Override
    public List<ElasticsearchRecord> deal(ElasticsearchVo elasticsearchVo) {
        ChartVo vo = new ChartVo();
        vo.setPage(0);
        vo.setLimit(10);
        vo.setName(elasticsearchVo.getName());
        PageInfo page = chartService.pageChartEs(vo);
        return null == page ? null : page.getList();
    }

}
